import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { flyIn } from '../../../../animations/fly-in';
import {HttpRequest} from "../../../../service/httpRequest";

declare var $:any;
@Component({
  selector: 'app-assin-roles',
  templateUrl: './assin-roles.component.html',
  styleUrls: ['./assin-roles.component.css'],
  animations: [
    flyIn
  ]
})
export class AssinRolesComponent implements OnInit {

  data = [];
  accountId:string = '';

  constructor(private httpRequest:HttpRequest,private activeRoute:ActivatedRoute) { }

  ngOnInit() {
    this.activeRoute.params.subscribe(params=>{
      this.accountId = params['accountId'];
      this.getData();
    });
  }


  getData(){
    this.httpRequest.sendGet('roles/list',{account_id:this.accountId}).subscribe(res=>{
      this.data = res.result;
      $('#treeview-checkable').treeview({
        data: this.data,
        showIcon: false,
        showCheckbox: true
      });

      $('#treeview-checkable').treeview('expandAll', {silent: true});
    },err=>console.log(err));
  }

  save(){
    let selectedNodes =  $('#treeview-checkable').treeview("getChecked");

    let str = '';
    for (let i = 0;i < selectedNodes.length; i++){
      str += selectedNodes[i].id +';';
    }
    str = str.substring(0, str.length - 1);
    this.httpRequest.sendPost('account/related',{
      role_ids:str,
      account_id:this.accountId
    }).subscribe(res=>{
      alert("角色分配成功！");
      this.getData();
    },err=>alert("角色分配失败！"));
    console.log(str)

  }
}
